import express from 'express';
import { PathMappingService } from '../services/PathMappingService.js';

const router = express.Router();

/**
 * 获取Docker环境信息
 * GET /api/docker/info
 */
router.get('/info', (req, res) => {
  try {
    const dockerInfo = PathMappingService.getDockerInfo();
    
    res.json({
      success: true,
      data: {
        ...dockerInfo,
        message: dockerInfo.isDocker 
          ? 'Docker环境已检测到，路径映射功能已启用' 
          : '非Docker环境，使用原始路径'
      }
    });
  } catch (error: any) {
    res.status(500).json({
      success: false,
      error: `获取Docker信息失败: ${error.message}`
    });
  }
});

/**
 * 测试路径映射
 * POST /api/docker/test-path
 */
router.post('/test-path', (req, res) => {
  try {
    const { path: testPath } = req.body;
    
    if (!testPath) {
      return res.status(400).json({
        success: false,
        error: '请提供要测试的路径'
      });
    }
    
    const mappingInfo = PathMappingService.getPathMappingSuggestion(testPath);
    const dockerInfo = PathMappingService.getDockerInfo();
    
    res.json({
      success: true,
      data: {
        originalPath: testPath,
        mappedPath: mappingInfo.mappedPath,
        isValid: mappingInfo.isValid,
        suggestion: mappingInfo.suggestion,
        dockerInfo
      }
    });
  } catch (error: any) {
    res.status(500).json({
      success: false,
      error: `路径映射测试失败: ${error.message}`
    });
  }
});

export default router;